import {
  SAVE_ALLCHANNELS,
  SAVE_ARTICLES,
  SAVE_CHANNELS,
} from '../actionType/home'

const initValue = {
  channels: [],
  allChannels: [],
  // 存储所有的文章列表
  articles: {},
  moreAction: {
    visible: false,
    articleId: '',
  },
}
export default function reducer(state = initValue, action) {
  const { type, payload } = action

  switch (type) {
    //   channles频道数据
    case SAVE_CHANNELS:
      return {
        ...state,
        channels: payload,
      }
    case SAVE_ALLCHANNELS:
      return {
        ...state,
        allChannels: payload,
      }
    case SAVE_ARTICLES:
      const { list, timestamp, isLoadMore, channelId } = payload
      return {
        ...state,
        articles: {
          ...state.articles,
          [channelId]: {
            timestamp: timestamp,
            list: isLoadMore
              ? [...state.articles[channelId].list, ...list]
              : list,
          },
        },
      }
    case 'home/feedback_action':
      return {
        ...state,
        moreAction: payload,
      }
    default:
      return state
  }
}
